﻿#include "ffmpeg_command.h"

// for Andriod, this may be help.
// https://github.com/guardianproject/android-ffmpeg


FFmpegCommand::FFmpegCommand()
{

}

// https://stackoverflow.com/questions/27145238/create-thumbnail-from-video-using-ffmpeg
QStringList FFmpegCommand::thumbnails(const QString& infile, const QString& outfile)
{
    // ffmpeg -i input.mp4 -ss 00:00:01.000 -vframes 1 output.png
    QStringList ret;
    ret << "-y" << "-i" << infile <<  "-ss" << "00:00:01.000" << "-vframes" << "1" << outfile;
    return ret;
}

// https://stackoverflow.com/questions/27145238/create-thumbnail-from-video-using-ffmpeg
QStringList FFmpegCommand::videoScreenShot(const QString &infile, const QString& outfile, const QString &time)
{
    // ffmpeg -i input.mp4 -ss 00:00:01.000 -vframes 1 output.png
    QStringList ret;
    ret << "-y" << "-i" << infile << "-ss" << time << "-vframes" << "1" << outfile;
    return ret;
}

// https://stackoverflow.com/questions/52673706/how-to-crop-and-scale-correctly-with-ffmpeg/52675535
QStringList FFmpegCommand::cropAndCutVideo(const QString &infile, const QString &outfile)
{
    // ffmpeg -y -i "test.avi" -filter:v "crop=iw-400:ih-40,scale=960:720" -pix_fmt yuv420p output_video.mp4
    QStringList ret;
    ret << "-y";
    ret << "-ss" << "00:00:00.910";
    ret << "-to" << "00:00:10.910";
    ret << "-i" << infile;
    //ret << "-filter:v";
    //ret << "crop=iw-400:ih-40,scale=960:720";
    //ret << "-pix_fmt" << "yuv420p";
    ret << outfile;
    return ret;
}

// https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg
QStringList FFmpegCommand::mergeVideo(const QStringList &infile, const QString &outfile)
{
    // ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \
    // -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] \
    // concat=n=3:v=1:a=1 [v] [a]" \
    // -map "[v]" -map "[a]" output.mkv

    QStringList ret;
    ret << "-y";
    foreach (QString file, infile) {
        ret << "-i" << file;
    }
    ret << "-filter_complex";

    QString concatStr;
    for(int i=0; i < infile.size(); ++i) {
        concatStr += QString("[%0:v] [%0:a] ").arg(i);
    }
    concatStr += QString("concat=n=%0:v=1:a=1 [v] [a]").arg(infile.size());
    ret << concatStr;
    ret << "-map" << "[v]" << "-map" << "[a]";
    ret << outfile;

    return ret;
}

// https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
QStringList FFmpegCommand::videoToGif(const QString &infile, const QString &outfile,
                                      const QString &beginTime, const QString &endTime,
                                      const QString &scale)
{
    // ffmpeg -ss 30 -t 3 -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
    QStringList ret;
    ret << "-y";
    ret << "-ss" << beginTime;
    ret << "-to" << endTime;
    ret << "-i" << infile;
    ret << "-vf" << QString("fps=10,scale=%0:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse").arg(scale); // 注意scale必须为偶数
    ret << "-loop" << "0";
    ret << outfile;

    return ret;
}

QStringList FFmpegCommand::convertVideo(const QString &infile, const QString &outfile)
{
    // ffmpeg -i infile outfile
    QStringList ret;
    ret << "-i";
    ret << infile;
    ret << outfile;

    return ret;
}
